<template>
  <div class="app-container">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <el-input v-model="filter.addr" placeholder="打卡地点" size="small" clearable />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.reset('filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList()">查 询</el-button>
          <el-button type="primary" size="small" @click="handleAdd()">新 增</el-button>
          <el-button type="primary" size="small" @click="handleTime()">作息时间</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="打卡地点" width="260px" prop="addr" />
      <el-table-column label="经度" align="center" width="120px" prop="longitude" />
      <el-table-column label="纬度" align="center" width="120px" prop="latitude" />
      <el-table-column label="备注" min-width="260px" prop="remark" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleStaff(row)">人 员</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    <Staffs ref="staffs" :get-data="getList" :info="dialog" />
    <Time ref="time" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { punchs, delPunch } from '@/api/punch'
import Dialog from './components/punch-dialog'
import Time from './components/time-dialog'
import Staffs from './components/staffs-dialog'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination, Dialog, Staffs, Time },
  inject: ['app'],
  data() {
    return {
      list: [{name: 1}],
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      this.app.loading = true
      punchs({
        ...this.filter,
        ...e
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delPunch({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleTime(e) {
      this.dialog.title = '作息时间'
      this.dialog.type = 'edit'
      e = {}
      this.app.dialogParam(this.dialog.edit, ['begin_time', 'end_time'], e)
      this.$refs['time'].visible = true
    },
    handleStaff(e) {
      this.dialog.title = '打卡人员'
      this.dialog.type = 'list'
      let data = {}
      data.lops_id = e.id
      this.app.dialogParam(this.dialog.edit, ['lops_id'], data)
      this.$refs['staffs'].visible = true
    },
    handleEdit(row) {
      this.dialog.title = '编辑信息'
      this.dialog.type = 'edit'
      this.app.dialogParam(this.dialog.edit, ['id', 'position', 'addr', 'remark', 'longitude', 'latitude'], row)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增信息'
      this.dialog.type = 'add'
      e = {}
      this.app.dialogParam(this.dialog.edit, ['id', 'position', 'addr', 'remark', 'longitude', 'latitude'], e)
      this.$refs['dialog'].visible = true
    }
  }
}
</script>
